package com.study.talk.app.group;


import android.net.Uri;
import android.os.Bundle;


import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;

import com.bumptech.glide.Glide;

import com.study.talk.app.base.BaseActivity;
import com.study.talk.databinding.ActivityCreateGroupInfoBinding;
import com.study.talk.entity.UserInfo;
import com.study.talk.entity.vo.SelectUserListVo;
import com.study.talk.net.NetApi;
import com.study.talk.netty.NettyClient;
import com.study.talk.netty.message.request.CreateGroupRequest;
import com.study.talk.netty.message.response.CreateGroupResponse;
import com.study.talk.util.DateUtil;
import com.study.talk.net.OkHttp;
import com.study.talk.util.FileUtil;
import com.study.talk.util.GsonInstance;
import com.study.talk.util.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class CreateGroupInfoActivity extends BaseActivity {


    private CreateGroupRequest createGroupRequest;

    private List<UserInfo> selectedList;
    private ActivityResultLauncher<PickVisualMediaRequest> selectPhoto;
    private ActivityResultLauncher<Uri> takePhoto;

    private ActivityCreateGroupInfoBinding activityCreateGroupInfoBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityCreateGroupInfoBinding = ActivityCreateGroupInfoBinding.inflate(getLayoutInflater());
        setContentView(activityCreateGroupInfoBinding.getRoot());

        selectedList = GsonInstance.instance.fromJson(getIntent().getStringExtra("data"), SelectUserListVo.class).getUserInfoList();
        selectPhoto = registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
            if (uri != null) {
                uploadImage(FileUtil.getPathFromUri(CreateGroupInfoActivity.this, uri));
            }
        });
        takePhoto = registerForActivityResult(new ActivityResultContracts.TakePicture(), result -> {
            if (result) {
                uploadImage(FileUtil.getPathFromUri(CreateGroupInfoActivity.this, uri));
            }
        });
        initView();
    }

    private void initView() {

        activityCreateGroupInfoBinding.createGroupInfoToolbar.setNavigationOnClickListener(v -> finish());

        createGroupRequest = new CreateGroupRequest();
        activityCreateGroupInfoBinding.createGroupBt.setOnClickListener(view -> {
            if (checkInput(activityCreateGroupInfoBinding.createGroupName, "请输入群聊名称"))
                return;
            if (checkInput(activityCreateGroupInfoBinding.createGroupTag, "请输入群聊标签")) return;
            if (checkInput(activityCreateGroupInfoBinding.createGroupIntroduce, "请输入群聊简介"))
                return;


            createGroupRequest.setGroupName(editTrim(activityCreateGroupInfoBinding.createGroupName));
            createGroupRequest.setTag(editTrim(activityCreateGroupInfoBinding.createGroupTag));
            createGroupRequest.setIntroduce(editTrim(activityCreateGroupInfoBinding.createGroupIntroduce));
            createGroupRequest.setMainUser(getApp().userId());
            createGroupRequest.setUserList(selectedList);
            createGroupRequest.setDateTime(DateUtil.getCurrentTime());

            NettyClient.sendMessage(createGroupRequest);


        });
        activityCreateGroupInfoBinding.createGroupImg.setOnClickListener(view -> showSelectPhotoDialog(selectPhoto, takePhoto));

    }

    public void uploadImage(String path) {
        NetApi.uploadImage(path, CreateGroupInfoActivity.this, data -> {
            createGroupRequest.setAvatar(data);
            runOnUiThread(() -> {
                Glide.with(CreateGroupInfoActivity.this).load(OkHttp.URL + data).into(activityCreateGroupInfoBinding.createGroupImg);
                ToastUtil.showToast(CreateGroupInfoActivity.this, "图片上传成功！");
            });
        }, msg -> runOnUiThread(() -> ToastUtil.showToast(getApplicationContext(), msg)));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCreateGroupMessage(CreateGroupResponse createGroupResponse) {
        ToastUtil.showToast(CreateGroupInfoActivity.this, "创建群聊成功！");
        updateGroupList();
    }


}